#include "GuiScreen.hpp"
/****/
void GuiScreen::initGui()
{
    selectedButton = NULL ;
}
/****/
void GuiScreen::updateScreen(){}
/****/
void GuiScreen::onGuiClosed(){}
/****/
void GuiScreen::drawScreen()
{
    for (int i= 0; i<buttonList.size(); i++)
    {
        GuiButton *button =  buttonList[i];
        button->draw(m_core);
    }
}

void GuiScreen::drawDebugScreen()
{
    m_core->printDebugInfos();
}
/****/
void GuiScreen::actionPerformed(GuiButton* guiButton){}
/****/
void GuiScreen::handleInput(sf::Event* event)
{
    handleMouseInput(event);
}

void GuiScreen::handleMouseInput(sf::Event* event)
{
    if(event->type == sf::Event::MouseButtonPressed)
    {
        if(event->mouseButton.button == sf::Mouse::Left)
        {
            onPLClick(event);
        }
    }
    else if(event->type == sf::Event::MouseButtonReleased)
    {
        if(event->mouseButton.button == sf::Mouse::Left)
        {
            onRLClick(event);
        }
    }
    else if(event->type == sf::Event::MouseMoved)
    {
        onMMove(event);
    }
}

void GuiScreen::onMMove(sf::Event* event)
{
    if(selectedButton != NULL)
    {
        if(selectedButton->getState() != -1)
        {
            selectedButton->setState(0);
        }
    }
    for(int i = 0; i< buttonList.size(); i++)
    {
        GuiButton* button = buttonList[i];
        sf::IntRect buttonRect = button->getRect();
        if(event->mouseMove.x > buttonRect.left &&
           event->mouseMove.y > buttonRect.top &&
           event->mouseMove.x < (buttonRect.left + buttonRect.width) &&
           event->mouseMove.y < (buttonRect.top + buttonRect.height))
        {
            std::cout<<button->getId()<<std::endl;
            selectedButton = button;
            if(button->getState() != -1)
            {
                selectedButton->setState(1);
            }
        }
    }
}

void GuiScreen::onPLClick(sf::Event* event)
{
    if(selectedButton != NULL)
    {
        if(selectedButton->getState() != -1)
        {
            selectedButton->setState(2);
        }
    }
    //selectedButton->launchSound();
}

void GuiScreen::onRLClick(sf::Event* event)
{
    if(selectedButton != NULL)
    {
        if(selectedButton->getState() != -1)
        {
            sf::IntRect buttonRect = selectedButton->getRect();
            if(event->mouseButton.x > buttonRect.left &&
                   event->mouseButton.y > buttonRect.top &&
                   event->mouseButton.x < (buttonRect.left + buttonRect.width) &&
                   event->mouseButton.y < (buttonRect.top + buttonRect.height))
            {
                selectedButton->setState(0);
                actionPerformed(selectedButton);

            }
        }
    }
}

/****/
bool GuiScreen::doesGuiPauseGame()
{
    return true;
}
